$(function (){
    loadFileTable();
    getAllFileType();
});

/**
 * 上传文件
 */
function uploadFile() {
    let files = $("#file").prop("files");
    console.log(files);
    let data = new FormData;
    data.append("file", files[0]);
    $.ajax({
        type: "POST",
        url: "/webapi/student/upload",
        data: data,
        processData: false, //无须序列化
        contentType: false, //无须处理
        cache: false //无须缓存
    }).done(function (data){
        if (data === 1){
            alert("文件上传成功，等待老师审核");
            loadFileTable();
        } else if (data === -1){
            alert("文件上传失败");
        }
    });
}

/**
 * 获取用户选择的文件名
 */
function getSelectFilename(){
    let file = $("#file").val();
    $("#uploadFilename").val(file);
}

/**
 * 加载文件列表
 * @param page 页码数
 */
function loadFileTable(page) {
    let filename = $("#filename").val().trim();
    let type = $("#type").val().trim();
    //console.log("page:" + page);
    $.ajax({
        type: "GET",
        url: "webapi/file/getByPage",
        data: {
            filename: filename,
            type: type,
            page: page
        }
    }).done(function (pageUtil){
        let table = $("#fileTable");
        let pagination = $("#pagination");
        //先清空表格
        table.empty();
        pagination.empty();
        console.log(pageUtil);
        let list = pageUtil.list;
        //插入文件信息
        for (const key in list) {
            let $file = list[key];
            let filename = $("<td title=" + $file.filename + ">" + fixedStringLength($file.filename) + "</td>");
            //转换文件大小单位
            let size = $("<td>" + ($file.size / (1024 * 1024)).toFixed(2) + "MB</td>");
            let type = $("<td>" + $file.type + "</td>");
            //格式化时间
            let updateDate = $("<td>" + formatDate($file.updateDate) + "</td>");
            let download = $("<a href=file/download?id=" + $file.id + " class='button small'>下载</a>");
            let fileInfo = $("<tr></tr>").append(filename).append(size).append(type).append(updateDate).append(download);
            table.append(fileInfo);
        }
        //页码处理
        let current = pageUtil.current;
        let totalPage = pageUtil.totalPage;
        let total = pageUtil.total;
        pagination.append("<li><span class='button small' onclick='loadFileTable(0)'>首页</span></li>")
        if (current > 0){
            pagination.append("<li><span class='button small' onclick='loadFileTable(" + (current - 1) + ")'>上一页</span></li>");
        }
        for (let i = 0; i < totalPage; i++) {
            let pageNum = $("<li><span class='page' onclick='loadFileTable(" + i + ")'>" + (i + 1) + "</span></li>");
            if (i === current){
                pageNum.children("span").addClass("active");
            }
            pagination.append(pageNum);
        }
        if (current < totalPage - 1){
            pagination.append("<li><span class='button small' onclick='loadFileTable(" + (current + 1) + ")'>下一页</span></li>");
        }
        pagination.append("<li><span class='button small' onclick='loadFileTable(" + (totalPage - 1) + ")'>尾页</span></li>");
        pagination.append("<li>共有" + total + "个文件" + "</li>");
    });
}

/**
 * 获取所有文件类型
 */
function getAllFileType() {
    $.ajax({
        type: "GET",
        url: "webapi/file/getAllType",
    }).done(function (allType) {
        for (const key in allType) {
            //console.log(allType[key]);
            $("#type").append("<option>" + allType[key] + "</option>")
        }
    })
}